Blockly.Blocks['hw_iot_package'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('导入 iot.hw 包为 hwiot');
        this.setOutput(false);
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.setNextStatement(true);
        this.setPreviousStatement(true);
        this.setTooltip('加载iot.hw自定义包');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['hw_iot_server_url'] = {
    init: function() {
        this.appendDummyInput()
            .appendField("获取 华为MQTT服务器地址 ");
        this.setOutput(true, "String");
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.setNextStatement(false);
        this.setPreviousStatement(false);
        this.setTooltip("华为MQTT服务器默认地址：iot-mqtts.cn-north-4.myhuaweicloud.com");
        this.setHelpUrl('');
    }
};

Blockly.Blocks['hw_iot_client_id'] = {
    init: function() {
        this.appendValueInput('DEVICE')
            .appendField("生成 客户端ID 设备 ")
            .setCheck('String');
        this.setOutput(true, "String");
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.setNextStatement(false);
        this.setPreviousStatement(false);
        this.setTooltip("格式为：设备ID_0_0_yyyyMMddHH");
        this.setHelpUrl('');
    }
};

Blockly.Blocks['hw_iot_encrypt_password'] = {
    init: function() {
        this.appendValueInput('PASSWORD')
            .appendField("生成 登录密码 设备密码 ")
            .setCheck("String");
        this.setOutput(true, "String");
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.setNextStatement(false);
        this.setPreviousStatement(false);
        this.setTooltip("使用时间（yyyyMMddHH）加密设备密码");
        this.setHelpUrl('');
    }
};

Blockly.Blocks['hw_iot_device_commad_topic'] = {
    init: function() {
        this.appendValueInput('DEVICE')
            .appendField("获取 设备命令主题 ")
            .setCheck("String");
        this.setOutput(true, "String");
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.setNextStatement(false);
        this.setPreviousStatement(false);
        this.setTooltip("设备命令主题格式为：\"$oc/devices/\" + deviceId + \"/sys/commands/#\"");
        this.setHelpUrl('');
    }
};

Blockly.Blocks['hw_iot_device_report_topic'] = {
    init: function() {
        this.appendValueInput('DEVICE')
            .appendField("获取 设备上报主题 ")
            .setCheck("String");
        this.setOutput(true, "String");
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.setNextStatement(false);
        this.setPreviousStatement(false);
        this.setTooltip("设备命令主题格式为：\"$oc/devices/\" + deviceId + \"/sys/properties/report\"");
        this.setHelpUrl('');
    }
};

// Blockly.Blocks['hw_iot_device_report_data'] = {
//     init: function() {
//         this.appendValueInput('SERVICE')
//             .appendField("生成 设备上报数据 服务 ")
//             .setCheck("String");
//         this.appendValueInput('PROPERTY')
//             .appendChild('属性 ')
//             .setAlign(Blockly.ALIGN_RIGHT)
//             .setCheck('String');
//         this.appendValueInput('VALUE')
//             .appendChild('值 ');
//         this.setOutput(true);
//         this.setColour('%{BKY_HW_IOT_HUE}');
//         this.setNextStatement(false);
//         this.setPreviousStatement(false);
//         this.setTooltip("设备命令主题格式为：\"$oc/devices/\" + deviceId + \"/sys/properties/report\"");
//         this.setHelpUrl('');
//     }
// };


Blockly.Blocks['hw_iot_device_report_service'] = {
    init: function () {
        this.appendValueInput('SERVICE')
            .appendField('创建 上报数据 服务 ')
            .setCheck('String');

        this.setHelpUrl("");
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.itemCount_ = 1;
        this.updateShape_();
        this.setOutput(true);
        this.setMutator(new Blockly.Mutator(['hw_iot_device_report_service_property']));
        this.setTooltip("创建上报数据");
    },

    mutationToDom: function () {
        var container = Blockly.utils.xml.createElement('mutation');
        container.setAttribute('items', this.itemCount_);
        return container;
    },

    domToMutation: function (xmlElement) {
        this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
        this.updateShape_();
    },

    decompose: function (workspace) {
        var containerBlock = workspace.newBlock('hw_iot_device_report_service_property_container');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('STACK').connection;
        for (var i = 0; i < this.itemCount_; i++) {
            var itemBlock = workspace.newBlock('hw_iot_device_report_service_property');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },

    compose: function (containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        // Count number of inputs.
        var connections = [];
        while (itemBlock) {
            connections.push(itemBlock.valueConnection_);
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
        // Disconnect any children that don't belong.
        for (var i = 0; i < this.itemCount_; i++) {
            var connection = this.getInput('PROPERTY' + i).connection.targetConnection;
            if (connection && connections.indexOf(connection) == -1) {
                connection.disconnect();
            }
        }
        this.itemCount_ = connections.length;
        this.updateShape_();
        // Reconnect any child blocks.
        for (var i = 0; i < this.itemCount_; i++) {
            Blockly.Mutator.reconnect(connections[i], this, 'PROPERTY' + i);
        }
    },

    saveConnections: function (containerBlock) {
        var itemBlock = containerBlock.getInputTargetBlock('STACK');
        var i = 0;
        while (itemBlock) {
            var input = this.getInput('PROPERTY' + i);
            itemBlock.valueConnection_ = input && input.connection.targetConnection;
            i++;
            itemBlock = itemBlock.nextConnection &&
                itemBlock.nextConnection.targetBlock();
        }
    },

    updateShape_: function () {
        // }
        // Add new inputs.
        for (var i = 0; i < this.itemCount_; i++) {
            if (!this.getInput('PROPERTY' + i)) {
                var input = this.appendValueInput('PROPERTY' + i)
                    .appendField('属性' + (i + 1) + ' ')
                    .setAlign(Blockly.ALIGN_RIGHT)
                    .setCheck('String');
            }
            if (!this.getInput('VALUE' + i)) {
                var input = this.appendValueInput('VALUE' + i)
                    .appendField('属性值' + (i + 1) + ' ')
                    .setAlign(Blockly.ALIGN_RIGHT);
            }
        }
        // Remove deleted inputs.
        while (this.getInput('PROPERTY' + i)) {
            this.removeInput('PROPERTY' + i);
            this.removeInput('VALUE' + i);
            i++;
        }
    }
};

Blockly.Blocks['hw_iot_device_report_service_property_container'] = {
    init: function () {
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.appendDummyInput()
            .appendField("设备属性列表");
        this.appendStatementInput('STACK');
        this.setTooltip('设备属性和属性值的容器');
        this.contextMenu = false;
    }
};

Blockly.Blocks['hw_iot_device_report_service_property'] = {
    init: function() {
        this.setColour('%{BKY_HW_IOT_HUE}');
        this.appendDummyInput()
            .appendField('属性和属性值');
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip('属性和属性值');
        this.contextMenu = false;
    }
};

